﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Fx.Data.NHibernate;
using System.Configuration;
using System.Web.Hosting;

namespace SBSK.Frontend.Code
{
    public class DbInitializer
    {
        private static bool wasNHibernateInitialized = false;

        /// <summary>
        /// Private, static object used only for synchronization
        /// </summary>
        private static object lockObject = new object();

        public static void Initialize(HttpApplication app)
        {
            if (!wasNHibernateInitialized)
            {
                lock (lockObject)
                {
                    if (!wasNHibernateInitialized)
                    {

                        var storage = new WebSessionStorage(app);
                        var mapAssemblies = new string[] { HostingEnvironment.MapPath("~/bin/SBSK.Entities.dll") };
                        var cfgFile = HostingEnvironment.MapPath("~/bin/nhibernate.config");
                        SessionConfigurer.Init(storage, mapAssemblies, cfgFile);
                        wasNHibernateInitialized = true;
                    }
                }
            }
        }
    }
}